package com.fingard.dsp.bank.directbank.hzcb01;


import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class HZCB01SinglePay extends HZCB01Base implements ISinglePay {

    //单笔支付，不需采用流处理方式
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        tmpRetDetail = externalPayments(p_req1908);
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    //190802对外支付
    private RetAtsSglDetail externalPayments(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        if (p_req1908.singleDetail.sameBnk.equals("1")) {//同行
            bankInnerTransfer(p_req1908, retDetail);
        } else {//跨行
            crossBankTransfer(p_req1908, retDetail);
        }
        return retDetail;
    }

    //同行支付
    private RetAtsSglDetail bankInnerTransfer(ReqSingle p_req1908, RetAtsSglDetail retDetail) throws Exception {

        txCode = "PY4002";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入报文头部信息
        writeCommonHeader(xmlWriter);
        XElement tmpXElement = new XElement("input", "");
        tmpXElement.addChild(new XElement("compFlowNo", p_req1908.singleDetail.askNumber));
        tmpXElement.addChild(new XElement("payAccount", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("payAccountName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("recAccount", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("recAccountName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("payAmount", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("currencyType", convertCurToBank(p_req1908.singleDetail.payCur)));
        tmpXElement.addChild(new XElement("payUse", p_req1908.singleDetail.abstractATS));
        tmpXElement.addChild(new XElement("payRem", p_req1908.singleDetail.purpose));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndElement();

        String signXml = getDataSignData(xmlWriter);//获取签名数据
        if (signnatrue(signXml) == true) {
            xmlWriter.writeElementString("signData", signature);//进行签名
            xmlWriter.writeEndDocument();//报文结束
            String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送给银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retMsg");
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    //50：主机处理中 90：交易成功；91：交易失败；99：交易可疑
                    String stt = tmpBkReader.getFirstText("/HZBANKB2C/ebank/output/orderSTT");
                    retDetail.transState = StringHelper.hasAnyChar(stt) ? stt : tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retCode");
                    retDetail.payInfoCode = retDetail.transState;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpSign[1];
        }
        return retDetail;
    }

    //跨行支付
    private RetAtsSglDetail crossBankTransfer(ReqSingle p_req1908, RetAtsSglDetail retDetail) throws Exception {
        txCode = "PY4004";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter);
        XElement tmpXElement = new XElement("input", "");
        tmpXElement.addChild(new XElement("compFlowNo", p_req1908.singleDetail.askNumber));
        tmpXElement.addChild(new XElement("payAccount", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("payAccountName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("recAccount", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("recAccountName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("unionBankNo", p_req1908.singleDetail.cnapsCode));//收款行号
        tmpXElement.addChild(new XElement("payAmount", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("currencyType", convertCurToBank(p_req1908.singleDetail.payCur)));
        //0：普通1：加急
        if (!StringHelper.isNullOrEmpty(p_req1908.singleDetail.paySpd) && p_req1908.singleDetail.paySpd.equals("1")) {
            tmpXElement.addChild(new XElement("urgentFlag", "1"));
        } else {
            tmpXElement.addChild(new XElement("urgentFlag", "0"));
        }
        tmpXElement.addChild(new XElement("payUse", p_req1908.singleDetail.abstractATS));
        tmpXElement.addChild(new XElement("payRem", p_req1908.singleDetail.memo));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndElement();

        String signXml = getDataSignData(xmlWriter);//获取签名数据
        if (signnatrue(signXml) == true) {
            xmlWriter.writeElementString("signData", signature);//进行签名
            xmlWriter.writeEndDocument();//报文结束

            String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retMsg");
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    //50：主机处理中 90：交易成功；91：交易失败；99：交易可疑
                    String stt = tmpBkReader.getFirstText("/HZBANKB2C/ebank/output/orderSTT");
                    retDetail.transState = StringHelper.hasAnyChar(stt) ? stt : tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retCode");
                    retDetail.payInfoCode = retDetail.transState;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpSign[1];
        }
        return retDetail;
    }

    //1944 状态查询
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);

        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        txCode = "PY4006";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter);
        xmlWriter.writeStartElement("input");
        xmlWriter.writeElementString("compFlowNo", p_req1944.reqHeader.reqSeqID);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();
        try {
            String signXml = getDataSignData(xmlWriter);//获取签名数据
            if (signnatrue(signXml) == true) {
                xmlWriter.writeElementString("signData", signature);//进行签名
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retCode");
                        String returnMsg = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retMsg");
                        if (returnCode != null && isActiveRetCode(returnCode)) {
                            while (tmpBkReader.read("/HZBANKB2C/ebank/output")) {
                                //50：主机处理中 90：交易成功；91：交易失败；99：交易可疑
                                String state = tmpBkReader.readedXElement.getSingleText("orderSTT");//状态
                                retDetail.respCode = "0";
                                retDetail.respInfo = returnMsg;
                                retDetail.payInfoCode = state;
                                retDetail.transState = state;
                                retDetail.reqSeqID = p_req1944.askNumber;
                            }
                        } else if ("EBPY0025".equals(returnCode) && "订单不存在".equals(returnMsg)) {
                            retDetail.respCode = "0";
                            retDetail.respInfo = returnMsg;
                            retDetail.payInfoCode = returnCode;
                            retDetail.transState = returnCode;
                            retDetail.reqSeqID = p_req1944.askNumber;
                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = returnMsg;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSign[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
